package com.icinema.main;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.icinema.client.CinemaService;
import com.icinema.client.CinemaServiceAsync;
import com.icinema.vo.Cinema;
import com.icinema.vo.CinemaSubscription;
import com.icinema.vo.CinemaSubscriptionDetail;

public class AddCinemaSubscription extends Composite {
	private final CinemaServiceAsync cinemaService = GWT
	.create(CinemaService.class);	
	ListBox lstCinemas = new ListBox();
	FlexTable subscriptTable = new FlexTable();
	VerticalPanel panel = new VerticalPanel();
	public AddCinemaSubscription(){
		
		initWidget(panel);
		FlexTable formPanel = new FlexTable();
		formPanel.setText(0,0,"Select Cinema to Subscribe:");
		formPanel.setWidget(0,1,lstCinemas);
		Button btnAdd = new Button("Add");
		formPanel.setWidget(0,2,btnAdd);
		btnAdd.addMouseUpHandler(new MouseUpHandler(){

			@Override
			public void onMouseUp(MouseUpEvent event) {
				CinemaSubscription sub = new CinemaSubscription();
				sub.setCinemaId(lstCinemas.getValue(lstCinemas.getSelectedIndex()));
				cinemaService.addCinemaSubscription(sub,new AsyncCallback<Void>(){
					
					@Override
					public void onFailure(Throwable caught) {
						
					}

					@Override
					public void onSuccess(Void result) {
						refresh();
					}
					
				});
				
			}
			
		});
		panel.add(formPanel);
		panel.add(subscriptTable);
		
	}
	public void refresh(){
		setVisible(false);
		setVisible(true);
	}
	@Override
	public void setVisible(boolean visible) {
		if(visible == true){
			loadCinemaList();
			loadSubscriptions();
		}
		super.setVisible(visible);
	}
	
	class ImageDeleteMouseDownHandler implements  MouseDownHandler{
		private String subscriptId; 
		public ImageDeleteMouseDownHandler(String movieShowingId){
			this.subscriptId = movieShowingId;
		}
		@Override
		public void onMouseDown(MouseDownEvent event) {
			if(!Window.confirm("This will Delete the Subscription")){
				return;
			}
			cinemaService.delete(subscriptId, new AsyncCallback<Void>(){

				@Override
				public void onFailure(Throwable caught) {
				}

				@Override
				public void onSuccess(Void result) {
				}
			});
			refresh();
		}
	}
	
	public void loadSubscriptions(){
			
			cinemaService.getCinemaSubscriptions(new AsyncCallback<List<CinemaSubscriptionDetail>>(){

				
			@Override
			public void onFailure(Throwable caught) {

				
			}

			@Override
			public void onSuccess(List<CinemaSubscriptionDetail> result) {
				subscriptTable.removeAllRows();
				subscriptTable.getRowFormatter().setStyleName(0,"tableViewBrowseHeaders");
				subscriptTable.setText(0,0,"Cinema Name");
				
				for(int i=0;i<result.size();i++){
					subscriptTable.setText(i+1, 0,result.get(i).getCinema().getName());
					Image imgDelete = new Image("/images/delete.png");
					imgDelete.setStyleName("imgButton");
					String subscriptionId = result.get(i).getCinemaSubscription().getId();
					imgDelete.addMouseDownHandler(new ImageDeleteMouseDownHandler(subscriptionId));
					subscriptTable.setWidget(i+1,2, imgDelete);
					subscriptTable.getCellFormatter().setStyleName(i+1, 0, "tableCell");
				}					
				
			}
			
		});
	}
	public void loadCinemaList(){
		cinemaService.getCinemas(new AsyncCallback<List<Cinema>>(){

			@Override
			public void onFailure(Throwable caught) {
				
			}

			@Override
			public void onSuccess(List<Cinema> result) {
				lstCinemas.clear();
				for(Cinema cinema:result){
					lstCinemas.addItem(cinema.getName(),cinema.getId());
				}
			}
			
		});
	}	
}
